/*************************************
 * Created by WebStorm
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time :2024/2/5 18:18
 Desc :
 **************************************/
import type {Fn} from '@/libs/help.ts'
import {tryOnMounted, tryOnUnmounted, useDebounceFn} from '@vueuse/core'
interface WindowSizeOptions {
    once?: boolean;
    immediate?: boolean;
    listenerOptions?: AddEventListenerOptions | boolean;
}

export function useWindowSizeFn<T>( fn: Fn<T>, wait = 150, options?: WindowSizeOptions) {
    let handler = () => {
        fn();
    };
    handler = useDebounceFn(handler, wait);
    const start = () => {
        if (options && options.immediate) {
            handler();
        }
        window.addEventListener("resize", handler);
    };

    const stop = () => {
        window.removeEventListener("resize", handler);
    };
    tryOnMounted(() => {
        start();
    });

    tryOnUnmounted(() => {
        stop();
    });
    return [start, stop];
}